/*
 *  Copyright (c) 2020 Dmitry Kazakov <dimula73@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "KisBezierTransformMesh.h"

#include "kis_grid_interpolation_tools.h"


KisBezierTransformMesh::PatchIndex KisBezierTransformMesh::hitTestPatch(const QPointF& pt, QPointF* localPointResult) const {
    auto result = endPatches();

    const QRectF unitRect(0, 0, 1, 1);

    for (auto it = beginPatches(); it != endPatches(); ++it) {
        Patch patch = *it;

        if (patch.dstBoundingRect().contains(pt)) {
            const QPointF localPos = KisBezierUtils::calculateLocalPos(patch.points, pt);

            if (unitRect.contains(localPos)) {

                if (localPointResult) {
                    *localPointResult = localPos;
                }

                result = it;
                break;
            }
        }
    }

    return result.patchIndex();
}

void KisBezierTransformMesh::transformPatch(const KisBezierPatch& patch, const QPoint& srcQImageOffset, const QImage& srcImage, const QPoint& dstQImageOffset, QImage* dstImage)
{
    QVector<QPointF> originalPointsLocal;
    QVector<QPointF> transformedPointsLocal;
    QSize gridSize;

    patch.sampleRegularGrid(gridSize, originalPointsLocal, transformedPointsLocal, QPointF(8, 8));

    const QRect dstBoundsI = patch.dstBoundingRect().toAlignedRect();
    const QRect imageSize = QRect(dstQImageOffset, dstImage->size());
    //KIS_SAFE_ASSERT_RECOVER_NOOP(imageSize.contains(dstBoundsI));

    {
        GridIterationTools::QImagePolygonOp polygonOp(srcImage, *dstImage, srcQImageOffset, dstQImageOffset);

        GridIterationTools::RegularGridIndexesOp indexesOp(gridSize);
        GridIterationTools::iterateThroughGrid
            <GridIterationTools::AlwaysCompletePolygonPolicy>(polygonOp, indexesOp,
                gridSize,
                originalPointsLocal,
                transformedPointsLocal);
    }
}


void KisBezierTransformMesh::transformMesh(const QPoint& srcQImageOffset, const QImage& srcImage, const QPoint& dstQImageOffset, QImage* dstImage) const
{
    for (auto it = beginPatches(); it != endPatches(); ++it) {
        transformPatch(*it, srcQImageOffset, srcImage, dstQImageOffset, dstImage);
    }
}



QRect KisBezierTransformMesh::approxNeedRect(const QRect& rc) const
{
    QRect result = rc;

    for (auto it = beginPatches(); it != endPatches(); ++it) {
        KisBezierPatch patch = *it;

        if (patch.dstBoundingRect().intersects(rc)) {
            result |= patch.srcBoundingRect().toAlignedRect();
        }
    }

    return result;
}

QRect KisBezierTransformMesh::approxChangeRect(const QRect& rc) const
{
    QRect result = rc;

    for (auto it = beginPatches(); it != endPatches(); ++it) {
        const KisBezierPatch patch = *it;

        if (patch.srcBoundingRect().intersects(rc)) {
            result |= patch.dstBoundingRect().toAlignedRect();
        }
    }

    return result;
}

